								--[################# HEADER #################]--
AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include("shared.lua");




								--[################# SENT CODE ###############]--


/*This is the Resources that the entity will produce*/
local giveRes =
{
	{resName = "toxic waste", giveMin = 10, giveMax = 35, useRes = true},
}


/*This is the Resources that the entity need to use to opperate*/
local takeRes = 
{
	{Index = 1, resName = "energy", minAmount = 100, drawMin = 10, drawMax = 60, useRes = true, holds = false},
	{Index = 2, resName = "air", minAmount = 100, drawMin = 10, drawMax = 60, useRes = true, holds = false},
	{Index = 3, resName = "coolant", minAmount = 100, drawMin = 10, drawMax = 60, useRes = true, holds = false},
	{Index = 3, resName = "neutrions", minAmount = 100, drawMin = 10, drawMax = 60, useRes = true, holds = false},
}


/******************************************************/
/***************This is activated on Spawn****************/
/******************************************************/
function ENT:Initialize()
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	self.Entity:SetUseType(SIMPLE_USE);
	self:CreateWireInputs("Activate");
	self:CreateWireOutputs("Active","Planet No.");
	self.Status = false
		
//check that there is in fact a resource to take	
	for k,v in pairs(takeRes) do
		if(v.useRes) then
			self:AddResource(v.resName, 0);
		end

		if (not(v.useRes) and v.holds != false) then
			self:AddResource(v.resName, v.holds);
		end
	end

	
//check that there is in fact a resource to give	
	for k,v in pairs(giveRes) do
		if(v.useRes) then
			self:AddResource(v.resName, 0);
		end
	end

end


/******************************************************/
/*******When active, these functions will be proccessed*******/
/******************************************************/
function ENT:Proccess()
--------------------------------------------------------------------------------------	
/*Take Resources*/
	for k,v in pairs(takeRes) do
		if(v.useRes) then
			RD_ConsumeResource(self, v.resName, math.random(v.drawMin, v.drawMax))
		end
	end	

	
/*Give Resources*/
	for k,v in pairs(giveRes) do
		if(v.useRes) then
			inc = math.random(v.giveMin, v.giveMax)
			RD_SupplyResource(self, v.resName, math.random(v.giveMin, v.giveMax));
			self:SetWire("Planet No.", inc);
		end
	end
----------------------------------------------------------------------------------------	
	Make_Planet_Habitable(self)

end


/******************************************************/
/****************Updates the Overlay*********************/
/******************************************************/
function ENT:ShowOutput()
	local add = "Off";
	if(self.Status) then
		add = "On";
	end
	self:SetWire("Active", self.Status);
	self:SetWire("Planet", self.planet);
	self:SetOverlayText(self.PrintName.." ("..add..")");
end
 
 
/******************************************************/
/****************Handles the Wire Inputs*****************/
/******************************************************/
function ENT:TriggerInput(k,v)
	if(k=="Activate") then
		if((v or 0) >= 1) then
			self.Status = true;
		else
			self.Status = false ;
		end
	end
end


/******************************************************/
/****************This is the USE activator****************/
/******************************************************/
function ENT:Use()
	if(self.Status) then
		self.Status = false;
		self:SetWire("Planet No.", " None");
		Reset_Planet_Habitable(self)
	else
		self.Status = true;
		Reset_Planet_Habitable(self)
	end
end



/******************************************************/
/**********This is called when the entity is removed**********/
/******************************************************/
function ENT:OnRemove()
	Reset_Planet_Habitable(self)
end



/******************************************************/
/****This is the Think Function (carried out every interval)****/
/******************************************************/
function ENT:Think()

/*Check to see if we have enough resources*/
	for k,v in pairs(takeRes) do
		if(RD_GetResourceAmount(self, v.resName) < v.minAmount and v.useRes != false) then
			self.Status = false
			self:SetWire("Planet No.", " None");
			Reset_Planet_Habitable(self)
		end
			
	end

	if(self.Status == true) then
		self:Proccess()
	end
	
	if(self.teamNo == nil) then
		self.teamNo = self.ply:Team()
	end
		
	self:ShowOutput(self.Status);
	self.Entity:NextThink(CurTime()+0.2);
	return true;
end
